package com.zl.exam;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * 代码生成器
 */
public class ZlCode {

    public static void main(String[] args) {
        //需要构建一个代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略

        //1.全局配置
        GlobalConfig config = new GlobalConfig();
        String property = System.getProperty("user.dir");
        config.setOutputDir(property+"/src/main/java");//创建目录地址
        config.setAuthor("张忆菲");//设置作者
        config.setOpen(false);//是否打开文件夹
        config.setFileOverride(false);//是否覆盖
        config.setServiceName("%sService"); //去掉Service的I前缀
        config.setIdType(IdType.AUTO); //
        config.setDateType(DateType.ONLY_DATE);//设置日期格式
        config.setSwagger2(true);//
        mpg.setGlobalConfig(config);//提交配置

        //2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/bikesharing?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);

        mpg.setDataSource(dsc);

        //3.包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("exam");
        pc.setParent("com.zl");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);


        //4.策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setInclude("typeinfo","orderinfo","companyinfo"); //设置要映射的表名
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true); // 自动lombok；

        //自动填充
        TableFill ocrtime = new TableFill("ocrtime", FieldFill.INSERT);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(ocrtime);
        sc.setTableFillList(list);
        sc.setRestControllerStyle(true);
        sc.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2

        mpg.setStrategy(sc);

        mpg.execute();//执行
    }
}
